package com.red.wood.module.eval.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.springjdbc.CommonDao;
import com.red.wood.module.eval.dao.ApplicationFileDao;

@Repository
public class ApplicationFileDaoImpl implements ApplicationFileDao{

	@Autowired
	private CommonDao dao;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public void addBatch(List<Map<String, Object>> paramList) {
		String columns = "singleTaskId,name,path,type";
		String insertSql = "insert into t_eval_applicationcdn_file(singleTaskId,name,path,type) values (?,?,?,?)";
		dao.insertBatchTableData(paramList, insertSql, columns);
	}

	public long add(Map<String, Object> map){
		String columns = "singleTaskId,name,path,type";
		String sql = "insert into t_eval_applicationcdn_file(singleTaskId,name,path,type) values (?,?,?,?)";
		return dao.getInsertTableDataForJdbcPrimaryKey(map, sql, columns);
	}
	
	@Override
	public void delete(String singleTaskId, String type) {
		StringBuffer sql = new StringBuffer();
		if(StringUtils.isNotBlank(singleTaskId)){
			sql.append("delete from t_eval_applicationcdn_file where singleTaskId = "+ singleTaskId  + " and type=" + type);
		}
		dao.deleteTableData(sql.toString());
	}

	@Override
	public List<Map<String, Object>> getBySingleTaskId(String singleTaskId) {
		if(StringUtils.isNotBlank(singleTaskId)){
			String sql = "select id as id, singleTaskId as singleTaskId,name as name,path as path,type as type from t_eval_applicationcdn_file where singleTaskId= " + singleTaskId;
			List<Map<String,Object>> dataList =  jdbcTemplate.queryForList(sql.toString());
			return dataList;
		} 
		return null;
	}

	@Override
	public long getCountBySingleTaskId(String singleTaskId, String type) {
		if(StringUtils.isNotBlank(singleTaskId)){
			String sql = "select count(1) as count from t_eval_applicationcdn_file where singleTaskId = " + singleTaskId + " and type=" + type;
			List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql);
			if(dataList != null && !dataList.isEmpty()){
				return Long.valueOf(dataList.get(0).get("count").toString());
			}
		}
		return 0;
	}

	@Override
	public Map<String, Object> getById(String id) {
		if(StringUtils.isNotBlank(id)){
			String sql = "select id as id, singleTaskId as singleTaskId,name as name,path as path,type as type from t_eval_applicationcdn_file where id = " + id ;
			List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql);
			if(dataList != null && !dataList.isEmpty()){
				return dataList.get(0);
			}
		}
		return null;
	}

}
